#include "mainwindow.h"
#include "fluxwindow.h"
#include "savewindow.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) : QWidget(parent)
{
    QGridLayout *layout = new QGridLayout(this);
    area = new QScrollArea(this);
    area->setBackgroundRole(QPalette::Shadow);

    this->framLabel = new QLabel(this);
    this->controller = new VideoController(this->framLabel, this);
    this->framLabel->setAlignment(Qt::AlignCenter);
    area->setWidget(this->framLabel);
    area->setWidgetResizable(true);

    this->tool = new QToolBar;
    this->bPlayPause = new QToolButton;
    QToolButton *bRecord = new QToolButton;
    QToolButton *bFullScreen = new QToolButton;
    QToolButton *bStop = new QToolButton;
    QToolButton *bOpenFile = new QToolButton;
    QToolButton *bOpenCamera = new QToolButton;
    QMenuBar *menuBar = new QMenuBar(this);
    QMenu *menuFile = new QMenu("Fichier");
    //QMenu *menuOption = new QMenu("Options");
    QAction *actionFlux = new QAction("Ouvrir un flux réseau", this);
    QAction *actionQuit = new QAction("&Quitter", this);

    QAction *actionSaveFolder = new QAction("Options d'enregistrement", this);

    menuFile->addAction(actionFlux);
    menuFile->addAction(actionQuit);

    menuBar->addMenu(menuFile);
    menuBar->addSeparator();
    menuBar->setGeometry(0,0,2000,25);

    tool->setGeometry(0,0,200,20);

    bPlayPause->setGeometry(0,0,10,20);
    bOpenCamera->setGeometry(0,0,10,20);
    bOpenFile->setGeometry(0,0,010,20);
    bFullScreen->setGeometry(0,0,10,20);
    bRecord->setGeometry(0,0,10,20);
    bStop->setGeometry(0,0,10,20);

    bRecord->setIcon(QIcon(":/record"));
    bPlayPause->setIcon(QIcon(":/play"));
    bFullScreen->setIcon(QIcon(":/full_screen"));
    bStop->setIcon(QIcon(":stop"));
    bOpenFile->setIcon(QIcon(":openFile"));
    bOpenCamera->setIcon(QIcon(":cam"));

    tool->addWidget(bOpenCamera);
    tool->addSeparator();
    tool->addWidget(bPlayPause);
    tool->addSeparator();
    tool->addWidget(bStop);
    tool->addSeparator();
    tool->addWidget(bOpenFile);
    tool->addSeparator();
    tool->addWidget(bRecord);
    tool->addSeparator();
    tool->addWidget(bFullScreen);

    this->connect(bFullScreen,      SIGNAL(clicked()),              this, SLOT(slotFullScreen()));
    this->connect(bRecord,          SIGNAL(clicked()),              this, SLOT(slotRecord()));
    this->connect(bPlayPause,       SIGNAL(clicked()),              this, SLOT(slotPlay()));
    this->connect(bStop,            SIGNAL(clicked()),              this, SLOT(slotStop()));
    this->connect(bOpenFile,        SIGNAL(clicked()),              this, SLOT(slotOpenFile()));
    this->connect(bOpenCamera,      SIGNAL(clicked()),              this, SLOT(slotOpenCamera()));
    this->connect(actionQuit,       SIGNAL(triggered()),            this, SLOT(slotQuit()));
    this->connect(actionFlux,       SIGNAL(triggered()),              this, SLOT(slotFlux()));
    this->connect(actionSaveFolder, SIGNAL(triggered()),              this, SLOT(slotSaveFolder()));

    this->resize(640, 480);

    layout->addWidget(area, 0, 0);
    layout->addWidget(tool, 1, 0, Qt::AlignCenter | Qt::AlignBottom);
    this->setLayout(layout);

    this->_width = 640;
    this->_height = 480;
}

void MainWindow::slotQuit()
{
    exit(0);
}


void MainWindow::slotSaveFolder()
{
    SaveWindow *fenetre = new SaveWindow(this);
    fenetre->show();
}

void MainWindow::slotFlux()
{/*
    FluxWindow *fenetreFlux = new FluxWindow(this);
    fenetreFlux->exec();*/
    QString flux =  QInputDialog::getText(this, "Flux:", "Flux:");
    this->controller->PlayVideo(flux);
    this->bPlayPause->setIcon(QIcon(":pause"));
}

void MainWindow::slotOpenCamera()
{
    this->controller->PlayCamera();
    this->bPlayPause->setIcon(QIcon(":pause"));
    qDebug() << "Open Cam" ;
}

void MainWindow::slotOpenFile()
{
    QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),"",tr("Files (*.avi *.km)"));
    this->controller->PlayVideo(fileName);
    this->bPlayPause->setIcon(QIcon(":pause"));
    qDebug() << "OpenFile" << fileName;
}

void MainWindow::slotStop()
{
    this->controller->Stop();
    this->bPlayPause->setIcon(QIcon(":play"));
}

void MainWindow::slotPlay()
{
    QToolButton *widget =  (QToolButton*)sender();
    bool isInPause = this->controller->Pause();
    if (!isInPause)
        widget->setIcon(QIcon(":pause"));
    else
        widget->setIcon(QIcon(":play"));
}

void MainWindow::slotRecord()
{
    QToolButton *widget =  (QToolButton*)sender();
    this->controller->Record();
    if (this->controller->isRecording())
        widget->setIcon(QIcon(":stop_record"));
    else
        widget->setIcon(QIcon(":record"));
}

void MainWindow::resizeEvent(QResizeEvent* event)
{
    QSize size = area->size();
    size.setWidth(size.width() - 3);
    size.setHeight(size.height() - 3);

    this->controller->setImgSize(size);
}

void MainWindow::slotFullScreen()
{
    QToolButton *widget =  (QToolButton*)sender();
    if (this->isFullScreen())
    {
        widget->setIcon(QIcon(":full_screen"));
        this->showNormal();
    }
    else
    {
        widget->setIcon(QIcon(":exit_full_screen"));
        this->showFullScreen();
    }
    QSize size(this->size().width(), this->size().height() - tool->height() - 10);
    this->controller->setImgSize(size);
}

MainWindow::~MainWindow()
{

}
